package com.example.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.example.springboot.domain.ResponseResult;
import com.example.springboot.domain.Role;
import com.example.springboot.domain.User;
import com.example.springboot.domain.UserVo;
import com.example.springboot.service.UserService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /*
        用户分页&多条件组合查询方法
     */

    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo){

        PageInfo pageInfo = userService.findAllUserByPage(userVo);

        return  new ResponseResult(true,200,"分页多条件查询成功",pageInfo);

    }

    /*
    * 更新用户状态
    * */
    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(Integer userId, String status){

        boolean flag=userService.updateUserStatus(userId,status);
        if(flag) {
            return new ResponseResult(true,200,"更新成功",null);
        } else {
            return new ResponseResult(true,400,"更新失败",null);
        }

    }

    @RequestMapping("register")
    public ResponseResult register(@RequestBody User user) {
        boolean flag=userService.register(user);
        if(flag) {
            return new ResponseResult(true,200,"创建成功",null);
        } else {
            return new ResponseResult(true,200,"用户已存在",null);
        }
    }


    /*
        用户登陆
     */

    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {
        System.out.println(user);
        return userService.login(user);

//        if(user1 !=null){
//
//            // 保存用户id及access_token到session中
//            HttpSession session = request.getSession();
//            String access_token = UUID.randomUUID().toString();
//            System.out.println(access_token);
//            session.setAttribute("access_token",access_token);
//            session.setAttribute("user_id",user1.getId());
//            System.out.println();
//            // 将查询出来的信息响应给前台
//            Map<String, Object> map = new HashMap<>();
//            map.put("access_token",access_token);
//            map.put("user_id",user1.getId());
//
//            // 将查询出来的user,也存到map中
//            map.put("user",user1);
//
//          return new ResponseResult(true,1,"登陆成功",map);
//
//        }else {
//            return  new ResponseResult(true,400,"用户名密码错误",null);
//        }

    }




    /*
        分配角色（回显）
     */
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRelationRoleById(Integer id){

        List<Role> roleList = userService.findUserRelationRoleById(id);

        return  new ResponseResult(true,200,"分配角色回显成功",roleList);
    }


    /*
        分配角色
     */
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo){

        userService.userContextRole(userVo);

        return new ResponseResult(true,200,"分配角色成功",null);
    }


    /*
        获取用户权限，进行菜单动态展示
     */

    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request){

        // 1.获取请求头中的token
        String header_token = request.getHeader("Authorization");
        System.out.println(header_token);
        // 2.获取session中token
        String session_token = (String) request.getSession().getAttribute("access_token");
        System.out.println(session_token);
        // 3.判断token是否一致
        if(header_token.equals(session_token)){
            // 获取用户id
            Integer user_id = (Integer) request.getSession().getAttribute("user_id");
            // 调用service,进行菜单信息查询
            ResponseResult responseResult = userService.getUserPermissions(user_id);
            return responseResult;
        }else {
            ResponseResult responseResult = new ResponseResult(false, 400, "获取菜单信息失败", null);
            return responseResult;
        }


    }
}
